n = eval(input())
c = list(range(1, n + 1))
i = 1
j = 0
while len(c) > 1:
    if i % 7 == 0 or '7' in str(i):
        del c[j]
    else:
        j += 1
    if j == len(c):
        j = 0
    i += 1
print(c[0])